package com.sbp.message.service.impl;

import com.dap.dao.DaoSupport;
import com.sbp.message.api.entity.LocalRecordStatus;
import com.sbp.message.entity.ConsumerMessageRecord;
import com.sbp.message.service.ConsumerMessageRecordService;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ConsumerMessageRecordServiceImpl implements ConsumerMessageRecordService {

    private DaoSupport daoService;
    public void setDaoService(DaoSupport daoService) {
        this.daoService = daoService;
    }


    @Override
    public void insert(ConsumerMessageRecord record) {
        daoService.insert(record);
    }

    @Override
    public int updateStatus(String messageId, LocalRecordStatus status) {
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("status", status.getValue());
        return daoService.update(ConsumerMessageRecord.class, "updateStatusByMessageId", map);
    }

    @Override
    public ConsumerMessageRecord selectOne(String messageId) {
        if (StringUtils.isEmpty(messageId))
            return null;

        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        return daoService.selectOne(ConsumerMessageRecord.class, "selectByMessageId", map);
    }

    @Override
    public void delete(String messageId) {
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        daoService.delete(ConsumerMessageRecord.class, "deleteByMessageId", map);
    }

    @Override
    public int deleteRecordBeforeTime(Date date) {
        Map<String, Object> map = new HashMap<>();
        map.put("oldest_time", date);
        return daoService.delete(ConsumerMessageRecord.class, "deleteRecordBeforeTime", map);
    }

}
